package liuds.music;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.scwang.smartrefresh.header.TaurusHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.BezierRadarHeader;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import liuds.music.utils.Utils;

/**
 * Created by Administrator on 2018/5/24.
 */

@SuppressLint("ValidFragment")
public abstract class BaseFragment extends Fragment {

    private String title;

    protected View rootView;
    protected SmartRefreshLayout refresh;
    protected FrameLayout contentView;
    protected boolean isInitialize = false;
    protected boolean isViewCreated = false;

    private Snackbar tipBar;

    @SuppressLint("ValidFragment")
    public BaseFragment(String title){
        this.title = title;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_base,container,false);
        contentView = rootView.findViewById(R.id.contentView);
        LayoutInflater.from(getActivity()).inflate(getLayoutResouceId(),contentView,true);

        refresh = rootView.findViewById(R.id.refresh);


//        refresh.setColorSchemeResources(R.color.colorPrimary,R.color.colorPrimary);
//        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                refresh.setRefreshing(true);
//                loadData();
//            }
//        });
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                loadData();
            }
        });
        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(2000);
            }
        });

        initView(getArguments());

        isViewCreated = true;
        setUserVisibleHint(getUserVisibleHint());

        return rootView;
    }

    protected abstract void initView(Bundle state);

    private OnLoadDataListener onLoadDataListener =  new OnLoadDataListener() {
        @Override
        public void onLoadSuccess() {
            refresh.finishRefresh();
//            if(tipBar!=null){
//                tipBar.dismiss();
//            }

            isInitialize = true;
        }

        @Override
        public void onLoadError(String message) {
            refresh.finishRefresh(false);
//            if(tipBar == null){
//                tipBar = Snackbar.make(contentView,message,Snackbar.LENGTH_INDEFINITE).setAction("重新加载", new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        refresh.autoRefresh();
//                        loadData();
//                    }
//                });
//            }else {
//                tipBar.setText(message);
//            }
//            tipBar.show();
        }
    };

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if(tipBar!=null){
            tipBar.dismiss();
        }
        if(isVisibleToUser && isViewCreated && !isInitialize){
            if(refresh.isEnabled()){
                refresh.autoRefresh();
            }
            loadData(onLoadDataListener);
        }
    }

    protected void loadData(OnLoadDataListener onLoadDataListener){}

    protected void loadData(){
        loadData(onLoadDataListener);
    }

    public <T extends View> T findViewById(@IdRes int id) {
        return contentView.findViewById(id);
    }

    protected abstract int getLayoutResouceId();

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    protected interface OnLoadDataListener{
        void onLoadSuccess();
        void onLoadError(String message);
    }
}
